package com.suzhe.ymall.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import com.suzhe.ymall.system.pojo.SysResource;
import com.suzhe.ymall.system.service.ISysResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author suzhe
 * @since 2019-06-05
 */
@RestController
@RequestMapping("/sys/resource")
@Slf4j
public class SysResourceController{

    @Autowired
    ISysResourceService iSysResourceService;

    /**
     * 资源列表
     */
    @GetMapping("/list")
    public ApiResEntity list() {
        List<SysResource> resources = iSysResourceService.list(new QueryWrapper<SysResource>() //
                .orderByAsc("order_num"));
        List<SysResource> parentlist = new ArrayList<>();
        resources.forEach(item -> {
            if (item.getParentId() == 0){
                parentlist.add(item);
            }
        });
        parentlist.forEach(item -> {
            setChildren(item,resources);
        });
        return ApiResEntity.ok(parentlist);
    }

    /**
     * 递归设置子节点
     * @param sysResource
     * @param sysResources
     */
    public void setChildren(SysResource sysResource,List<SysResource> sysResources){
        List<SysResource> children = getChildren(sysResource.getId(), sysResources);
        if (!children.isEmpty()){
            sysResource.setChildren(children);
            children.forEach(item -> {
                setChildren(item,sysResources);
            });
        }
    }

    /**
     * 获取多有的孩子节点
     * @param parentId
     * @param sysResources
     * @return
     */
    public List<SysResource> getChildren(Integer parentId,List<SysResource> sysResources){
        List<SysResource> children = new ArrayList<>();
        sysResources.forEach(item -> {
            if (parentId == item.getParentId()){
                children.add(item);
            }
        });
        return children;
    }


    @PostMapping("/save")
    public ApiResEntity save(@RequestBody SysResource sysResource) {
        log.debug("sysResource:{}",sysResource);
        sysResource.setUpdateTime(new Date());
        if (sysResource.getId() == null){
            sysResource.setCreateTime(new Date());
            iSysResourceService.save(sysResource);
        }else{
            iSysResourceService.updateById(sysResource);
        }

        return ApiResEntity.ok();
    }

    @DeleteMapping("/del/{id}")
    public ApiResEntity del(@PathVariable Integer id) {
        List<SysResource> list = iSysResourceService.list(new QueryWrapper<SysResource>().eq("parent_id", id));
        if (!list.isEmpty()){
           return ApiResEntity.error("存在子资源不能删除");
        }
        iSysResourceService.removeById(id);
        return ApiResEntity.ok();
    }


    /**
     * 资源列表
     */
    @GetMapping("/getActionPerms")
    public Set<String> getActionPerms(@RequestParam("userId")  Integer userId) {
        return iSysResourceService.getActionPerms(userId);
    }


}
